package com.minimalist.lindi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.lang.Assert;
import com.minimalist.common.config.exception.BusinessException;
import com.minimalist.common.entity.lindi.enums.LindiIndexEnum;
import com.minimalist.common.entity.lindi.enums.LindiTradeApplicationOperatorEnum;
import com.minimalist.common.entity.lindi.po.LindiTradeApplicationOperator;
import com.minimalist.common.entity.lindi.vo.tradeapplicationoperator.LindiTradeApplicationOperatorVO;
import com.minimalist.common.utils.UnqIdUtil;
import com.minimalist.lindi.mapper.LindiTradeApplicationOperatorMapper;
import com.minimalist.lindi.service.LindiTradeApplicationOperatorService;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 林地指标平台_交易申请操作表 服务层实现。
 *
 * @author 11987
 * @since 2025-04-24
 */
@Service
public class LindiTradeApplicationOperatorServiceImpl extends
        ServiceImpl<LindiTradeApplicationOperatorMapper, LindiTradeApplicationOperator>
        implements LindiTradeApplicationOperatorService{

    @Autowired
    private LindiTradeApplicationOperatorMapper mapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addTradeApplicationOperator(LindiTradeApplicationOperatorVO operatorVO){
        LindiTradeApplicationOperator operator = BeanUtil.copyProperties(
                operatorVO, LindiTradeApplicationOperator.class);
        operator.setTradeApplicationOperatorId(UnqIdUtil.uniqueId());
        int insert = mapper.insert(operator, true);
        Assert.isTrue(insert == 1, () -> new BusinessException(
                LindiTradeApplicationOperatorEnum.ErrorMsgEnum
                        .ADD_TRADE_APPLICATION_OPERATOR_ERROR.getDesc()));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteTradeApplicationOperatorByTradeApplicationOperatorId(
            Long tradeApplicationOperatorId){
        LindiTradeApplicationOperator lindiTradeApplicationOperator = mapper
                .selectTradeApplicationOperatorByTradeApplicationOperatorId(
                        tradeApplicationOperatorId);
        Assert.notNull(lindiTradeApplicationOperator, () -> new BusinessException(
                LindiTradeApplicationOperatorEnum.ErrorMsgEnum
                        .NONENTITY_TRADE_APPLICATION_OPERATOR.getDesc()));
        int delete = mapper.delete(lindiTradeApplicationOperator);
        Assert.isTrue(delete == 1, () -> new BusinessException(
                LindiTradeApplicationOperatorEnum.ErrorMsgEnum
                        .DELETE_TRADE_APPLICATION_OPERATOR_ERROR.getDesc()));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateTradeApplicationOperatorByTradeApplicationOperatorId(
            LindiTradeApplicationOperatorVO operatorVO){
        LindiTradeApplicationOperator operator = mapper
                .selectTradeApplicationOperatorByTradeApplicationOperatorId(
                        operatorVO.getTradeApplicationOperatorId());
        Assert.notNull(operator, () -> new BusinessException(LindiIndexEnum.ErrorMsgEnum
                .NONENTITY_INDEX.getDesc()));
        BeanUtil.copyProperties(operatorVO, operator
                , CopyOptions.create().ignoreNullValue());
        int update = mapper.update(operator);
        Assert.isTrue(update == 1, () -> new BusinessException(
                LindiTradeApplicationOperatorEnum.ErrorMsgEnum
                        .UPDATE_TRADE_APPLICATION_OPERATOR_ERROR.getDesc()));
    }

    @Override
    public List<LindiTradeApplicationOperatorVO> getTradeApplicationOperatorsByApplicationId(
            Long tradeApplicationId){
        List<LindiTradeApplicationOperator> operatorList = mapper
                .selectTradeApplicationOperatorsByApplicationId(tradeApplicationId);
        return BeanUtil.copyToList(operatorList, LindiTradeApplicationOperatorVO.class);
    }
}
